
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/tutorial/basic_operations.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:23:54 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Basic Operations &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
    <script src="../_static/jquery.js"></script>
    <script src="../_static/underscore.js"></script>
    <script src="../_static/doctools.js"></script>
    <script src="../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../live.sympy.org/static/live-sphinx.js"></script>
    <script async="async" src="../../../cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest8331.js?config=TeX-AMS_HTML-full"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]]}})</script>
    
    <link rel="shortcut icon" href="../_static/sympy-notailtext-favicon.ico"/>
    <link href="basic_operations.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Printing" href="printing.html" />
    <link rel="prev" title="Gotchas" href="gotchas.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="printing.html" title="Printing"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="gotchas.html" title="Gotchas"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">SymPy Tutorial</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Basic Operations</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="basic-operations">
<span id="tutorial-basic"></span><h1>Basic Operations<a class="headerlink" href="#basic-operations" title="Permalink to this headline">¶</a></h1>
<p>Here we discuss some of the most basic operations needed for expression
manipulation in SymPy.  Some more advanced operations will be discussed later
in the <a class="reference internal" href="manipulation.html#tutorial-manipulation"><span class="std std-ref">advanced expression manipulation</span></a> section.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s2">&quot;x y z&quot;</span><span class="p">)</span>
</pre></div>
</div>
<section id="substitution">
<h2>Substitution<a class="headerlink" href="#substitution" title="Permalink to this headline">¶</a></h2>
<p>One of the most common things you might want to do with a mathematical
expression is substitution.  Substitution replaces all instances of something
in an expression with something else.  It is done using the <code class="docutils literal notranslate"><span class="pre">subs</span></code> method.
For example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="go">cos(y) + 1</span>
</pre></div>
</div>
<p>Substitution is usually done for one of two reasons:</p>
<ol class="arabic">
<li><p>Evaluating an expression at a point. For example, if our expression is
<code class="docutils literal notranslate"><span class="pre">cos(x)</span> <span class="pre">+</span> <span class="pre">1</span></code> and we want to evaluate it at the point <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">0</span></code>, so that
we get <code class="docutils literal notranslate"><span class="pre">cos(0)</span> <span class="pre">+</span> <span class="pre">1</span></code>, which is 2.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="go">2</span>
</pre></div>
</div>
</li>
<li><p>Replacing a subexpression with another subexpression.  There are two
reasons we might want to do this.  The first is if we are trying to build
an expression that has some symmetry, such as <span class="math notranslate nohighlight">\(x^{x^{x^x}}\)</span>.  To build
this, we might start with <code class="docutils literal notranslate"><span class="pre">x**y</span></code>, and replace <code class="docutils literal notranslate"><span class="pre">y</span></code> with <code class="docutils literal notranslate"><span class="pre">x**y</span></code>.  We
would then get <code class="docutils literal notranslate"><span class="pre">x**(x**y)</span></code>.  If we replaced <code class="docutils literal notranslate"><span class="pre">y</span></code> in this new expression
with <code class="docutils literal notranslate"><span class="pre">x**x</span></code>, we would get <code class="docutils literal notranslate"><span class="pre">x**(x**(x**x))</span></code>, the desired expression.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">x</span><span class="o">**</span><span class="n">y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">x**y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">expr</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">x**(x**y)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">expr</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">x</span><span class="o">**</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">x**(x**(x**x))</span>
</pre></div>
</div>
<p>The second is if we want to perform a very controlled simplification, or
perhaps a simplification that SymPy is otherwise unable to do.  For
example, say we have <span class="math notranslate nohighlight">\(\sin(2x) + \cos(2x)\)</span>, and we want to replace
<span class="math notranslate nohighlight">\(\sin(2x)\)</span> with <span class="math notranslate nohighlight">\(2\sin(x)\cos(x)\)</span>.  As we will learn later, the function
<code class="docutils literal notranslate"><span class="pre">expand_trig</span></code> does this.  However, this function will also expand
<span class="math notranslate nohighlight">\(\cos(2x)\)</span>, which we may not want.  While there are ways to perform such
precise simplification, and we will learn some of them in the
<a class="reference internal" href="manipulation.html#tutorial-manipulation"><span class="std std-ref">advanced expression manipulation</span></a> section, an
easy way is to just replace <span class="math notranslate nohighlight">\(\sin(2x)\)</span> with <span class="math notranslate nohighlight">\(2\sin(x)\cos(x)\)</span>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">cos</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expand_trig</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
<span class="go">2*sin(x)*cos(x) + 2*cos(x)**2 - 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">sin</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">),</span> <span class="mi">2</span><span class="o">*</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="go">2*sin(x)*cos(x) + cos(2*x)</span>
</pre></div>
</div>
</li>
</ol>
<p>There are two important things to note about <code class="docutils literal notranslate"><span class="pre">subs</span></code>.  First, it returns a
new expression.  SymPy objects are immutable.  That means that <code class="docutils literal notranslate"><span class="pre">subs</span></code> does
not modify it in-place.  For example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">cos(x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span>
<span class="go">x</span>
</pre></div>
</div>
<aside class="sidebar">
<p class="sidebar-title">Quick Tip</p>
<p>SymPy expressions are immutable.  No function will change them in-place.</p>
</aside>
<p>Here, we see that performing <code class="docutils literal notranslate"><span class="pre">expr.subs(x,</span> <span class="pre">0)</span></code> leaves <code class="docutils literal notranslate"><span class="pre">expr</span></code> unchanged.
In fact, since SymPy expressions are immutable, no function will change them
in-place.  All functions will return new expressions.</p>
<p>To perform multiple substitutions at once, pass a list of <code class="docutils literal notranslate"><span class="pre">(old,</span> <span class="pre">new)</span></code> pairs
to <code class="docutils literal notranslate"><span class="pre">subs</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="n">z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">subs</span><span class="p">([(</span><span class="n">x</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="p">(</span><span class="n">z</span><span class="p">,</span> <span class="mi">0</span><span class="p">)])</span>
<span class="go">40</span>
</pre></div>
</div>
<p>It is often useful to combine this with a list comprehension to do a large set
of similar replacements all at once.  For example, say we had <span class="math notranslate nohighlight">\(x^4 - 4x^3 + 4x^2 -
2x + 3\)</span> and we wanted to replace all instances of <span class="math notranslate nohighlight">\(x\)</span> that have an even power
with <span class="math notranslate nohighlight">\(y\)</span>, to get <span class="math notranslate nohighlight">\(y^4 - 4x^3 + 4y^2 - 2x + 3\)</span>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">x</span><span class="o">**</span><span class="mi">4</span> <span class="o">-</span> <span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">replacements</span> <span class="o">=</span> <span class="p">[(</span><span class="n">x</span><span class="o">**</span><span class="n">i</span><span class="p">,</span> <span class="n">y</span><span class="o">**</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="k">if</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">replacements</span><span class="p">)</span>
<span class="go">-4*x**3 - 2*x + y**4 + 4*y**2 + 3</span>
</pre></div>
</div>
</section>
<section id="converting-strings-to-sympy-expressions">
<h2>Converting Strings to SymPy Expressions<a class="headerlink" href="#converting-strings-to-sympy-expressions" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">sympify</span></code> function (that’s <code class="docutils literal notranslate"><span class="pre">sympify</span></code>, not to be confused with
<code class="docutils literal notranslate"><span class="pre">simplify</span></code>) can be used to convert strings into SymPy expressions.</p>
<p>For example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">str_expr</span> <span class="o">=</span> <span class="s2">&quot;x**2 + 3*x - 1/2&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">sympify</span><span class="p">(</span><span class="n">str_expr</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">x**2 + 3*x - 1/2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">19/2</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p><code class="docutils literal notranslate"><span class="pre">sympify</span></code> uses <code class="docutils literal notranslate"><span class="pre">eval</span></code>.  Don’t use it on unsanitized input.</p>
</div>
</section>
<section id="evalf">
<h2><code class="docutils literal notranslate"><span class="pre">evalf</span></code><a class="headerlink" href="#evalf" title="Permalink to this headline">¶</a></h2>
<p>To evaluate a numerical expression into a floating point number, use
<code class="docutils literal notranslate"><span class="pre">evalf</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span>
<span class="go">2.82842712474619</span>
</pre></div>
</div>
<p>SymPy can evaluate floating point expressions to arbitrary precision.  By
default, 15 digits of precision are used, but you can pass any number as the
argument to <code class="docutils literal notranslate"><span class="pre">evalf</span></code>.  Let’s compute the first 100 digits of <span class="math notranslate nohighlight">\(\pi\)</span>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pi</span><span class="o">.</span><span class="n">evalf</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span>
<span class="go">3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068</span>
</pre></div>
</div>
<p>To numerically evaluate an expression with a Symbol at a point, we might use
<code class="docutils literal notranslate"><span class="pre">subs</span></code> followed by <code class="docutils literal notranslate"><span class="pre">evalf</span></code>, but it is more efficient and numerically
stable to pass the substitution to <code class="docutils literal notranslate"><span class="pre">evalf</span></code> using the <code class="docutils literal notranslate"><span class="pre">subs</span></code> flag, which
takes a dictionary of <code class="docutils literal notranslate"><span class="pre">Symbol:</span> <span class="pre">point</span></code> pairs.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">evalf</span><span class="p">(</span><span class="n">subs</span><span class="o">=</span><span class="p">{</span><span class="n">x</span><span class="p">:</span> <span class="mf">2.4</span><span class="p">})</span>
<span class="go">0.0874989834394464</span>
</pre></div>
</div>
<p>Sometimes there are roundoff errors smaller than the desired precision that
remain after an expression is evaluated. Such numbers can be removed at the
user’s discretion by setting the <code class="docutils literal notranslate"><span class="pre">chop</span></code> flag to True.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">one</span> <span class="o">=</span> <span class="n">cos</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">sin</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">one</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span>
<span class="go">-0.e-124</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">one</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">evalf</span><span class="p">(</span><span class="n">chop</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">0</span>
</pre></div>
</div>
</section>
<section id="lambdify">
<h2><code class="docutils literal notranslate"><span class="pre">lambdify</span></code><a class="headerlink" href="#lambdify" title="Permalink to this headline">¶</a></h2>
<p><code class="docutils literal notranslate"><span class="pre">subs</span></code> and <code class="docutils literal notranslate"><span class="pre">evalf</span></code> are good if you want to do simple evaluation, but if
you intend to evaluate an expression at many points, there are more efficient
ways.  For example, if you wanted to evaluate an expression at a thousand
points, using SymPy would be far slower than it needs to be, especially if you
only care about machine precision.  Instead, you should use libraries like
<a class="reference external" href="http://www.numpy.org/">NumPy</a> and <a class="reference external" href="http://www.scipy.org/">SciPy</a>.</p>
<p>The easiest way to convert a SymPy expression to an expression that can be
numerically evaluated is to use the <code class="docutils literal notranslate"><span class="pre">lambdify</span></code> function.  <code class="docutils literal notranslate"><span class="pre">lambdify</span></code> acts
like a <code class="docutils literal notranslate"><span class="pre">lambda</span></code> function, except it converts the SymPy names to the names of
the given numerical library, usually NumPy.  For example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> 
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> 
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">lambdify</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">expr</span><span class="p">,</span> <span class="s2">&quot;numpy&quot;</span><span class="p">)</span> 
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> 
<span class="go">[ 0.          0.84147098  0.90929743  0.14112001 -0.7568025  -0.95892427</span>
<span class="go"> -0.2794155   0.6569866   0.98935825  0.41211849]</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p><code class="docutils literal notranslate"><span class="pre">lambdify</span></code> uses <code class="docutils literal notranslate"><span class="pre">eval</span></code>.  Don’t use it on unsanitized input.</p>
</div>
<p>You can use other libraries than NumPy. For example, to use the standard
library math module, use <code class="docutils literal notranslate"><span class="pre">&quot;math&quot;</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">lambdify</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">expr</span><span class="p">,</span> <span class="s2">&quot;math&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
<span class="go">0.0998334166468</span>
</pre></div>
</div>
<p>To use lambdify with numerical libraries that it does not know about, pass a
dictionary of <code class="docutils literal notranslate"><span class="pre">sympy_name:numerical_function</span></code> pairs.  For example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">mysin</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="gp">... </span>    <span class="sd">&quot;&quot;&quot;</span>
<span class="gp">... </span><span class="sd">    My sine. Note that this is only accurate for small x.</span>
<span class="gp">... </span><span class="sd">    &quot;&quot;&quot;</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">lambdify</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">expr</span><span class="p">,</span> <span class="p">{</span><span class="s2">&quot;sin&quot;</span><span class="p">:</span><span class="n">mysin</span><span class="p">})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
<span class="go">0.1</span>
</pre></div>
</div>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Basic Operations</a><ul>
<li><a class="reference internal" href="#substitution">Substitution</a></li>
<li><a class="reference internal" href="#converting-strings-to-sympy-expressions">Converting Strings to SymPy Expressions</a></li>
<li><a class="reference internal" href="#evalf"><code class="docutils literal notranslate"><span class="pre">evalf</span></code></a></li>
<li><a class="reference internal" href="#lambdify"><code class="docutils literal notranslate"><span class="pre">lambdify</span></code></a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="gotchas.html"
                        title="previous chapter">Gotchas</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="printing.html"
                        title="next chapter">Printing</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/tutorial/basic_operations.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="printing.html" title="Printing"
             >next</a> |</li>
        <li class="right" >
          <a href="gotchas.html" title="Gotchas"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" >SymPy Tutorial</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Basic Operations</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/tutorial/basic_operations.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:23:54 GMT -->
</html>